﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using Rally_like_a_madman.Data;
using System.Windows.Navigation;

namespace Rally_like_a_madman
{
    public partial class AddNewWaypointPage : PhoneApplicationPage
    {
        public ObservableCollection<RouteWaypointLink> routeWaypointLinks;
        public Waypoint waypointToAdd;
        public Route activeRoute;

        public AddNewWaypointPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddWaypoint_Click(object sender, RoutedEventArgs e)
        {
            if (tbName != null && tbName.Text != "Naam van de Waypoint" && tbLongitude.Text != string.Empty && tbLatitude.Text != string.Empty)
            {
                if (NavigationHelper.CheckIfWaypointExists(tbName.Text))
                    MessageBox.Show("Een Waypoint met deze naam bestaat al. Geef dit Waypoint een andere naam om verder te gaan.");
                else
                {
                    waypointToAdd.Name = tbName.Text;
                    waypointToAdd.GPSLongitude = float.Parse(tbLongitude.Text);
                    waypointToAdd.GPSLatitude = float.Parse(tbLatitude.Text);
                    waypointToAdd.Description = tbDescription.Text;

                    //Add the Waypoint to the database
                    App thisApp = Application.Current as App;
                    thisApp.ActiveDB.WaypointTable.InsertOnSubmit(waypointToAdd);
                    thisApp.ActiveDB.SubmitChanges();

                    //Create a new RouteWaypointLink and add it to the ObservableCollection
                    RouteWaypointLink routeWaypointLink = new RouteWaypointLink();
                    routeWaypointLink.Route = activeRoute;
                    routeWaypointLink.Waypoint = waypointToAdd;
                    routeWaypointLink.WaypointIndex = routeWaypointLinks.Count;
                    routeWaypointLinks.Add(routeWaypointLink as RouteWaypointLink);

                    NavigationService.GoBack();
                }
            }
            else
            {
                MessageBox.Show("Vul een naam, een lengtegraad en een breedtegraad toe voordat je een Waypoint aanmaakt.");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbName_GotFocus(object sender, RoutedEventArgs e)
        {
            if (tbName.Text == "Naam van de Waypoint")
                tbName.Text = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbName_LostFocus(object sender, RoutedEventArgs e)
        {
            if (tbName.Text == string.Empty)
                tbName.Text = "Naam van de Waypoint";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateWaypoint_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text != string.Empty && tbName.Text != "Naam van de Waypoint")
            {
                if (NavigationHelper.CheckIfWaypointExists(tbName.Text))
                    MessageBox.Show("Een Waypoint met deze naam bestaat al. Geef dit Waypoint een andere naam om verder te gaan.");
                else
                {
                    waypointToAdd = new Waypoint();
                    waypointToAdd.Name = tbName.Text;

                    btnCreateWaypoint.IsEnabled = false;
                    tbLongitude.IsEnabled = true;
                    tbLatitude.IsEnabled = true;
                    tbDescription.IsEnabled = true;
                    btnAddWaypoint.IsEnabled = true;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbLongitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                float.Parse(tbLongitude.Text);
            }
            catch (Exception)
            {
                tbLongitude.Text = string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbLatitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                float.Parse(tbLatitude.Text);
            }
            catch (Exception)
            {
                tbLatitude.Text = string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            AddNewWaypointLocationPage destionationExistingWaypointLocation = e.Content as AddNewWaypointLocationPage;
            if (destionationExistingWaypointLocation != null)
            {
                destionationExistingWaypointLocation.activeWaypoint = this.waypointToAdd;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (waypointToAdd != null)
            {
                if (waypointToAdd.GPSLongitude != 0 && waypointToAdd.GPSLatitude != 0)
                {
                    tbLongitude.Text = waypointToAdd.GPSLongitude.ToString();
                    tbLatitude.Text = waypointToAdd.GPSLatitude.ToString();
                }
            }

            base.OnNavigatedTo(e);
        }
          

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbLongitude_GotFocus(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddNewWaypointLocationPage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbLatitude_GotFocus(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddNewWaypointLocationPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}